<?
require_once('config.php');
/*------------- Xử lý tìm kiếm ------------------------*/
$action = getValue('action','str','POST','');
$current_page = getValue('page','int','GET',1);
$limit_page   = 20;
$start        =   ($current_page -1) * $limit_page; 
if($action == 'search'){
   $date_from = getValue('date_from','str','POST',0);
   $date_from = str_replace('/','-',$date_from);
   $date_to = getValue('date_to','str','POST',0);
   $date_to = str_replace('/','-',$date_to);
   $start_date = strtotime($date_from);
   $end_date = strtotime($date_to) +  86400;
   $sql_select = "SELECT   
                        search_id,
                        search_text,
                        search_keyword,
                        search_date,
                        search_count
                     FROM
                        search
                     WHERE 1 ";
   
   $sql_where = " AND search_date > " . $start_date . " AND search_date < " . $end_date . " ORDER BY search_count DESC, search_id DESC";
   $sql_search = $sql_select . $sql_where;
   $db_select  =  new db_query($sql_search);
   $array_search  = $db_select->resultArray();
   $total  = count($array_search);
}else{
   $array_search = $search::getList($start,$limit_page);
   $total = $search::getTotal();
   $pages = ceil($total/$limit_page);
}
/*----------------- Xử lý phân trang --------------------*/

$count_search = count($array_search); 



?>
<?=$css_global?>
<?=$js_global?>

<script>
   $(function() {
   $( ".datepicker" ).datepicker({
      dateFormat: 'dd/mm/yy',
   });
   $(document).on('click','.reload_tab' ,function(){
   window.location.reload();
   });
});
</script>
<div class="row-fluid">
   <div class="head">
      <div class="isw-grid"></div>
      <h1>Danh sách từ khóa</h1>
      <div class="reload_tab" title="Reload"></div>                              
      <div class="clear"></div>
   </div>                                
   <div class="block-fluid table-sorting">
      <div id="tSortable_wrapper" class="dataTables_wrapper" >
         <div class="dataTables_filter" id="tSortable_filter">
            <form method="POST" action="" style="margin: 0;">
               <table class="head-table">
                  <tr class="search">
                     <input type="hidden" name="action" id="action" value="search" />                     
                     <td><label>Từ ngày:</label></td>
                     <td><input class="datepicker" name="date_from" value="<?=date('d/m/Y', strtotime("-1 week"))?>" type="text" /></td>
                     <td><label>Đến ngày:</label></td>
                     <td><input class="datepicker" name="date_to" value="<?=date('d/m/Y', time())?>" type="text" /></td>
                     <td><input type="submit" class="bottom btn btn-info btn-small" value="Tìm kiếm" data-original-title="" title=""/></td>
                     <td>
                        
                     </td>
                  </tr>
                  <tr class="caption">
                     <td class="font12" colspan="2" style="text-align: left;"><b>Tổng số: <?=$total?></b></td>
                     <td class="font12" colspan="2" style="text-align: left;"><b>Trang số: <?=$current_page?></b></td>
                     <td class="font12" colspan="2" style="text-align: left;"><b>Mỗi trang: <?=$limit_page?></b></td>
                  </tr>
               </table>
            </form>
         </div>
         <table cellpadding="0" cellspacing="0" width="100%" class="table dataTable table-bordered" id="tSortable"  style="width: 100%;">
            <thead>
              <tr>
                  <th class="sorting_disabled"  rowspan="1" colspan="1" style="width: 23px;">
                     <div class="checker">
                        <span>
                           <input type="checkbox" name="checkall" style="opacity: 0;"/>
                        </span>
                     </div>
                  </th>
                  <th width="5%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">ID</th>
                  <th width="35%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Từ khóa</th>
                  <th width="25%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Không dấu</th>
                  <th width="20%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Ngày tạo</th>
                  <th width="10%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Số lần</th>

                  
               </tr>
            </thead>
              <?
              if($count_search > 0){
                  foreach($array_search as $value){   
                  ?>
                     <tr class="even" data-sid=<?=$value['search_id']?> >
                        <td class="font12 sorting_1">
                           <div class="checker">
                              <span>
                                 <input type="checkbox" class="checkbox-row" name="row_to_delete[]" value="<?=$value['search_id']?>" style="opacity: 0;"/>
                              </span>
                           </div>
                        </td>
                        <td class="font12"><?=$value['search_id']?></td>
                        <td class="font12 search-name"><?=$value['search_text']?></td>  
                        <td class="font12 search-name"><?=$value['search_keyword']?></td> 
                        <td class="font12"><?=($value['search_date'] > 0) ? date('H:i:s - d/m/Y', $value['search_date']) : 'Chưa rõ'?></td>      
                        <td class="font12"><?=$value['search_count']?></td>                    
                    </tr>
                  <?}?>
              <?}?>
          </table>
         </div>
      <div class="clear"></div>
      <div class="mult_submit pull-left">
         <span>Với những dòng đã chọn: </span>  <span class="delete-row mult-delete">Xóa</span> 
      </div>
      <?if($action != 'search'):?>
      <div class="paging pull-right">
         <?php $pagination = pagination($current_page,$pages,'list.php?page=%d') ?>
         <ul>
         <?php foreach ($pagination as $link): ?>
           <li><?php echo $link ?></li>
         <?php endforeach ?>
         </ul>
      </div>
      <?endif?>
   </div>
</div>
<script>
$(function(){
   
   $('.mult-delete').click(function(){
      var matches = [];
      $(".checkbox-row:checked").each(function() {
          matches.push(this.value);
          string_cat_id = matches.join(',');
      });
      data = {'id':string_cat_id};
      var updateHTML = function(data){
         if(data != ''){
            alert(data);
            window.location.reload();
         }
      }
      var beforeSend = function(){
         
      }
      var conf = confirm("Xóa những dòng đã chọn ?");
      if(conf)
         handleAjax('delete.php','POST',data,updateHTML,beforeSend);
   });
});
</script>